
<table id="address_data" style="position:relative;z-index:2"></table>

<script type="text/javascript">
	var obj;
	var editRow=undefined;//记录当前正在被编辑的行
	var currentOp;
	var flag;
	
	obj=$('#address_data').datagrid({
		url:'../back/address/select',
		fitColumns:true,
		striped:true,
		loadMsg:"数据加载中...",
		pagination:true,
		fit:true,
		pageNumber:1,
		pageSize:10,
		pageList:[10,20,30,40,50],
		sortName:'addrid',
		remoteSort:false,
		columns:[[
			{field:'addrids',title:'',width:100,align:'center',checkbox:true},
			{field:'addrid',title:'地址编号',width:100,align:'center',sortable:true},
			{field:'userid',title:'用户编号',width:100,align:'center',editor:{type:'text',option:{requires:true}}},
			{field:'address',title:'地址',width:100,align:'center',editor:{type:'text',option:{requires:true}}},
			{field:'tel',title:'电话',width:100,align:'center',editor:{type:'text',option:{requires:true}}},
			{field:'receiver',title:'收件人',width:100,align:'center',editor:{type:'text',option:{requires:true}}},
			{field:'createdate',title:'创建日期',width:100,align:'center',
				formatter:function(value,row,index){  
                    var Timestamp = new Date(value);  
                    return Timestamp.toLocaleString();  
                }  
			},
			{field:'createperson',title:'创建人',width:100,align:'center'},
			{field:'updatedate',title:'修改日期',width:100,align:'center',
				formatter:function(value,row,index){  
                    var unixTimestamp = new Date(value);  
                    return unixTimestamp.toLocaleString();  
                }  	
			},
			{field:'updateperson',title:'修改人',width:100,align:'center'},
			{field:'delflag',title:'是否删除',width:100,align:'center'},
			{field:'status',title:'状态',width:100,align:'center',
                formatter:function(value,row,index){  
                    if(value=='1'){
                        return '有效';
                    }else{
                        return '禁用'
                    }
                },
                editor:{
                    type:"combobox",
                    options:{
                        data:[{'key':'有效','value':'1'},{'key':'禁用','value':'0'}],
                        valueField: 'value',    
                        textField: 'key',    
                        panelHeight: 'auto',  
                        required: true ,  
                        editable:false 
                    }
                }
            }
		]],
		toolbar:[{
			text:"添加",
			iconCls:"icon-add",
			handler:function(){
				flag="添加";
				currentOp="insert";
				if (editRow != undefined) {
                    $("#address_data").datagrid('endEdit', editRow);
                }
                if (editRow == undefined) {
                    $("#address_data").datagrid('insertRow', {
                        index: 0,
                        row: {}
                    });
                    $("#address_data").datagrid('beginEdit', 0);
                    editRow = 0;
                }
			}
		},{
			text:'修改',
			iconCls:"icon-edit",
			handler:function(){
				currentOp="update";
				flag="修改";
				var rows=obj.datagrid("getChecked")[0];
				if(rows!=undefined){
					if(editRow!=undefined){//说明有行正在被编辑
						obj.datagrid("rejectChanges");//回滚所有数据
						obj.datagrid("endEdit",editRow);//关闭正在被编辑的行
						editRow=undefined;
					}else{
						//获取用户选中的行的索引
						var index=obj.datagrid("getRowIndex",rows);
						//console.info(rows);
						obj.datagrid("updateRow",{index:index,row:rows});
						obj.datagrid("beginEdit",index);
						editRow=index;//记录当前被编辑的行的索引
					}
				}else{
					$.messager.show({title:'温馨提示',msg:'请选中您要修改的数据.....',timeout:2000,showType:'slide'});
				}
			}
		},{
			text:'删除',
			iconCls:"icon-remove",
			onClick:function(){
			//获取选中的行
				var rows=obj.datagrid("getChecked");
				//alert(typeof rows[0].tid);
				if(rows[0]!=undefined){
					$.messager.confirm('信息确认', '您真的要删除选中的数据吗?', function(rs){
						if (rs){
							var addrids = "";
							for(var i=0;i<rows.length-1;i++){
								addrids += rows[i].addrid+",";//到这里 是对的，
							}
							addrids += rows[i].addrid;
							
							$.ajax({
								  type: 'DELETE',
								  url: "../back/address/"+addrid,//现在这个删除的方法不能删除多个，只能单个删除
								  dataType: "JSON",
								  success: function(data){
									  if(data.code=='0000'){
										  //成功
										  $.messager.show({title:'成功提示',msg:'地址标签删除成功....',timeout:2000,showType:'slide'});
										rows=null;
										obj.datagrid("reload");//刷新表格
									  }else{
										  $.messager.alert('失败提示','地址标签删除失败,请确认该类别下无新闻....','error');
									  }
								  }
							});
						}else{
							return;	
						}
					});
				}else{
					$.messager.show({title:'温馨提示',msg:'请选中您要删除的数据.....',timeout:2000,showType:'slide'});
				}
			}
		},{
			text:'保存',
			iconCls:"icon-save",
			handler:function(){
				//先关闭编辑
				obj.datagrid("endEdit",editRow);
				//获取正在被编辑的数据
				var rows=obj.datagrid("getChanges");
				//alert(rows);
				if(rows==undefined){//说明没有被保存的数据
					obj.datagrid("rejectChanges");//回滚所有数据
					obj.datagrid("endEdit",editRow);//关闭正在被编辑的行
					editRow=undefined;
				}else if(rows.address==""){
					editRow=undefined;
					obj.datagrid("rejectChanges");
				}else{
					//保存到数据库
					//rows["op"]=currentOp;
					var addrid=$(rows).attr("addrid");//这里用$()将一个js对象包裹起来，将js对象转换成一个jquery对象
					//jquery对象--》js对象    ===》1、$("#div").eq(0);  2、$("#div")[0]    
					//alert(rows);
					var addrid=$(rows).attr("addrid");
					var userid=$(rows).attr("userid");
					var address=$(rows).attr("address");
					var tel=$(rows).attr("tel");
					var receiver=$(rows).attr("receiver");
					var createdate=$(rows).attr("createdate");
					var createperson=$(rows).attr("createperson");
					var updatedate=$(rows).attr("updatedate");
					var updateperson=$(rows).attr("updateperson");
					var delflag=$(rows).attr("delflag");
					var status=$(rows).attr("status");
					$.ajax({
						  type: 'POST',
						  url: "../back/address/"+currentOp,
						  dataType: "JSON",
						  data: {"addrid":addrid,"userid":userid,"address":address,"tel":tel,"receiver":receiver,"createdate":createdate,
							  "createperson":createperson,"updatedate":updatedate,"updateperson":updateperson,"delflag":delflag,"status":status},
						  success: function(data){
							  if(data.code>0){
								  $.messager.show({
										title:'成功提示',
										msg:'地址标签'+flag+'成功....',
										timeout:2000,
										showType:'slide'
									});
									obj.datagrid("rejectChanges");//回滚所有数据
									rows=null;
									editRow=undefined;
									obj.datagrid("endEdit",editRow);//关闭正在被编辑的行
									obj.datagrid("reload");//刷新表格
							  }else{
								  $.messager.alert('失败提示','地址标签'+flag+'失败....','error');
							  }
						 }
					});
				}
			}
		},{
			text:'撤销',
			iconCls:"icon-redo",
			handler:function(){
				obj.datagrid("rejectChanges");//回滚所有数据
				obj.datagrid("endEdit",editRow);//关闭正在被编辑的行
				editRow=undefined;	
			}
		}]
	});
	
	
</script>
